
var app=getApp()
import{
  productLists,
  addProducts
} from "../../../../api.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iphoneX:false,
    currpage:1,
    lastpage:2,
    shoplist:[],
    imgurl:"http://document.growthupup.com/add_pic.png",
    outshop:[],
    num:0,
    price:0,
    i:-1,
    show:true,
    value:"",
    bottominput:false
  },
  close(){
    this.setData({
      show:true,
      value:""
    })
  },
  bottominput(){
    this.setData({
      bottominput:true
    })
  },
  save(){
    var token=app.globalData.token
    var arr=[]
    this.data.outshop.forEach(r=>{
      if(r!=undefined){
        arr.push({id:r.id,
          commission_price: r.commission_price
        })
      }
    })
    addProducts({
      token:token,
      products:arr
    }).then(res=>{
      console.log(res)
      if(res.data.code===666){
        wx.showToast({
          title: res.data.msg,
          icon:"none"
        })
        setTimeout(()=>{
          wx.navigateTo({
            url: '../../shopCar/shopCar',
          })
        },1000)
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
    
  },
  // 增删时计数
  remove(e){
    var that = this
    var id = e.currentTarget.dataset.id;
    var outshops = "shoplist[" + id + "].bol";
    that.data.outshop.splice(id,1,null)
    that.setData({
      [outshops]: false,
      num: that.data.num-1,
    })
  },
  add(e){
    var that=this
    var id = e.currentTarget.dataset.id;
    that.setData({
      show:false,
      i:id
    })
  },
  // 保存单个商品出品价
  price(e){
    console.log(e.detail.value)
    this.setData({
      price: e.detail.value
    })
  },
  confirm(){
    var i = this.data.i;
    var price = this.data.price
    this.data.outshop.splice(i, 1, {
      id: this.data.shoplist[i].id,
      commission_price: this.data.shoplist[i].price
    })
    var outshops = "shoplist[" + i + "].bol";
    var prices = "shoplist[" + i + "].commission_price"
    var shopprice = "outshop[" + i +"].commission_price";
    if(price==0){
      this.setData({
        show: true
      })
      wx.showToast({
        title: '请输入价格',
        icon:"none"
      })
     
      return
    }
    this.setData({
      [shopprice]:price,
      show: true,
      [outshops]:true,
      value:"",
      [prices]:price,
      num: this.data.num + 1
    })
  },
  products(page){
    var that = this;
    var token = app.globalData.token;
    productLists({
      token: token,
      page: page
    }).then(res => {
      if (res.data.code === 666) {
        console.log(res)
        res.data.data.data.forEach(r => {
          r.bol = false,
            that.data.outshop.push(null)
        })
        if(page==1){
          that.setData({
            shoplist: res.data.data.data,
            currpage: res.data.data.current_page,
            lastpage: res.data.data.last_page
          })
        } else if (page <= res.data.data.last_page){
          that.setData({
            shoplist: that.data.shoplist.concat(res.data.data.data),
            currpage: res.data.data.current_page,
            lastpage: res.data.data.last_page
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
       
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      iphoneX: app.globalData.is_iPhoneX
    })
    this.products(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.products(1);
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.currpage)
    if(this.data.currpage>this.data.lastpage){
      return
    }else{
      var page = this.data.currpage + 1
      this.products(page);
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})